package io.github.wjehovah.yyn.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.List;

import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.adapter.SubscriptDetailAdapter;
import io.github.wjehovah.yyn.bean.SubscriptItemBean;
import io.github.wjehovah.yyn.bean.SubscriptItemBean.SubscriptItemDetail;
import io.github.wjehovah.yyn.view.XListView;

/**
 * 订阅的详情界面
 * Created by yingchen on 2015/8/10.
 */
public class SubscriptDetailActivity extends Activity {
    //后退
    private TextView mBackTextView;

    //标题
    private TextView mTitleTextView;

    //订阅按钮
    private Button mAddButton;

    //详情列表
    private XListView mXListView;

    private List<SubscriptItemDetail> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subscript_detail_activity);
        Intent intent = getIntent();
        String url = intent.getStringExtra("SUBSCRIPT_DETAIL");
        String subscriptName = intent.getStringExtra("SUBSCRIPT_NAME");

        Log.e("SubscriptDetailActivity","url==="+url);

        //初始化布局
        initViews();

        //设置标题名称
        mTitleTextView.setText(subscriptName);

        //根据url请求网络获取数据
        getDataFromNet(url);
    }

    /**
     * 初始化布局
     */
    public void initViews(){
        mBackTextView = (TextView) findViewById(R.id.tv_subscript_detail_back);
        mTitleTextView = (TextView) findViewById(R.id.tv_subscript_detail_title);
        mAddButton = (Button) findViewById(R.id.btn_subscript_detail_add);
        mXListView = (XListView) findViewById(R.id.xlv_subscript_detail);
    }


    public void getDataFromNet(String url){
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //Log.e("SubscriptDetailActivity","请求数据成功"+responseInfo.result);
                //解析并处理json数据
                String json = responseInfo.result;
                processData(json);

            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(SubscriptDetailActivity.this,"加载数据失败,稍后重试",Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 解析并处理json数据
     * @param json
     */
    private void processData(String json) {
        SubscriptItemBean subscriptItemBean = new Gson().fromJson(json, SubscriptItemBean.class);
        Log.e("TAG","解析成功------"+subscriptItemBean.toString());

        //显示ListView
       //mXListView.setAdapter();
        data = subscriptItemBean.list;
        SubscriptDetailAdapter adapter = new SubscriptDetailAdapter(SubscriptDetailActivity.this,data);
        mXListView.setAdapter(adapter);
    }

    /**
     * 返回
     * @param view
     */
    public void back(View view){
        finish();
    }
}
